package powerBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2021/3/29.
 */
public class UserInfo implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();

        String sql1 = "SELECT username,power FROM `user`";
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        //拿到admin全部权限数据
        JSONObject power = new JSONObject();
        SqlReturnModel sqlReturnMode = sqlDispatch.selectWord(sql1);
        //拿到全部的用户数据
        JSONObject result = new JSONObject();

        JSONArray userPowerList = new JSONArray();
        for (ArrayList<String> one: sqlReturnMode.getSelectReturnData()){
            JSONObject userPower = new JSONObject();
            if (Objects.equals(one.get(0), "admin") || Objects.equals(one.get(0), "POWERLIST")){
                result.put("adminPower", JSONObject.parse(one.get(1)));
                continue;
            }
            userPower.put(one.get(0), JSONObject.parse(one.get(1)));
            userPowerList.add(userPower);
        }
        result.put("userPowerList", userPowerList);
        returnJson.setResult(result);

        return returnJson.toString();
    }
}
